package minh.bac.wcbrazil2014.favorite;

import java.util.ArrayList;

import minh.bac.wcbrazil2014.R;
import minh.bac.wcbrazil2014.WCTAG;
import minh.bac.wcbrazil2014.database.MySQLiteManager;
import minh.bac.wcbrazil2014.listviewmatch.ListViewMatchFragment;
import minh.bac.wcbrazil2014.object.FavoriteObj;
import minh.bac.wcbrazil2014.object.FootBallTeamObj;
import minh.bac.wcbrazil2014.object.MatchScheduleObj;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

public class FavoriteFragment extends Fragment {
	
	FavoriteObj favoriteObj;
	ArrayList<FootBallTeamObj> footBallTeamObjs;
	private MySQLiteManager mMySQLiteManager;
	static FragmentManager fragmentManager;
	public static FavoriteFragment newInstance()
	{
		FavoriteFragment fragment = new FavoriteFragment();
		return fragment;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.favorite_layout, container, false);
		//
		mMySQLiteManager = new MySQLiteManager(getActivity());
		footBallTeamObjs = mMySQLiteManager.getFavoriteTeam();
		fragmentManager = getChildFragmentManager();
		if( (footBallTeamObjs) != null && (footBallTeamObjs.size() != 0))
		{
			switchFragment(R.id.container_favorite, new ListViewTeamFM() , true , WCTAG.TAGFM_FAVORITE);
		}
		else
		{
			Toast.makeText(getActivity(), "Vui lòng chọn đội yêu thích", Toast.LENGTH_SHORT).show();
			switchFragment(R.id.container_favorite, new ChooseFavoriteTeamFM(), true, null);
		}
		 
		//
		return rootView;
	}
	public static void switchFragment(int idContainer, Fragment fragment ,boolean addToBackStack, String TAG)
	{
		if(addToBackStack)
		{
			if( TAG!= null )
			{
				fragmentManager.beginTransaction().replace(idContainer, fragment, TAG).addToBackStack(TAG).commit();
				
			}
			else
			{
				fragmentManager.beginTransaction().replace(idContainer, fragment).addToBackStack(null).commit();
			}
		}
		else
		{
			if( TAG!= null )
			{
				fragmentManager.beginTransaction().replace(idContainer, fragment, TAG).commit();
			}
			else
			{
				fragmentManager.beginTransaction().replace(idContainer, fragment).commit();
			}
		}
	}
}
